using System;
using System.Collections.Generic;
using System.Text;
using FlatRedBall;
using FlatRedBall.Input;
using FlatRedBall.AI.Pathfinding;
using FlatRedBall.Graphics.Animation;
using FlatRedBall.Graphics.Particle;

using FlatRedBall.Math.Geometry;
using FlatRedBall.Math.Splines;
using BitmapFont = FlatRedBall.Graphics.BitmapFont;
using Cursor = FlatRedBall.Gui.Cursor;
using GuiManager = FlatRedBall.Gui.GuiManager;

using FlatRedBall.Audio;

#if FRB_XNA || SILVERLIGHT
using Keys = Microsoft.Xna.Framework.Input.Keys;
using Vector3 = Microsoft.Xna.Framework.Vector3;
using Texture2D = Microsoft.Xna.Framework.Graphics.Texture2D;

using NereusGame.Entities.Projectiles;
#endif

namespace NereusGame.Entities.Weapons
{
	public partial class MissileLauncher
	{
        private const int MAX_AMMO = 15;
		private void CustomInitialize()
		{
            Name = "Missile";
            InitializeAmmo();
            fireRate = initialFireRate = 60;
		}

		private void CustomActivity()
		{


		}

		private void CustomDestroy()
		{

            base.Destroy();
		}

        private static void CustomLoadStaticContent(string contentManagerName)
        {


        }

        private void InitializeAmmo()
        {
            for (int i = 0; i < MAX_AMMO; i++)
                deadAmmo.Enqueue(new Missile(CONTEN_MANAGER));
        }

        protected override void Upgrade1()
        {
            damage = 14;
            defaultVelocity = 5;
        }

        protected override void Upgrade2()
        {
            damage += 2;
            fireRate = initialFireRate -= 5;
            defaultVelocity += 2;
        }

        protected override void Upgrade3()
        {
            damage += 2;
            fireRate = initialFireRate -= 5;
            defaultVelocity += 2;
        }

        protected override void Upgrade4()
        {
            damage += 2;
            fireRate = initialFireRate -= 5;
        }
       

        protected override void Upgrade5()
        {
            damage += 2;
        }



        public override void Shoot4(Vector3 pos, float offset, float rotation, Vector3 velocity)
        {
            if (state == WeaponState.ReadyToShoot)
            {
                // calculate direciton
                shootingPosition.X = (float)Math.Cos(rotation + (float)Math.PI / 2f) + pos.X;
                shootingPosition.Y = (float)Math.Sin(rotation + (float)Math.PI / 2f) + pos.Y;
                Vector3 direction = (shootingPosition - pos);
                direction.Normalize();

                // calculate shootingposition
                shootingPosition = pos + direction * offset;
                shoot2Bullets(direction, rotation, velocity);

                state = WeaponState.DownTime;
            }

        }

        public override void Shoot5(Vector3 pos, float offset, float rotation, Vector3 velocity)
        {
            if (state == WeaponState.ReadyToShoot)
            {
                // calculate direciton
                shootingPosition.X = (float)Math.Cos(rotation + (float)Math.PI / 2f) + pos.X;
                shootingPosition.Y = (float)Math.Sin(rotation + (float)Math.PI / 2f) + pos.Y;
                Vector3 direction = (shootingPosition - pos);
                direction.Normalize();


                // calculate shootingposition
                shootingPosition = pos + direction * offset;

                deadAmmo.Peek().Position = shootingPosition;  // this will set the position of the projectile
                deadAmmo.Peek().Shoot(Shooter, direction, rotation, velocity);
                aliveAmmo.Enqueue(deadAmmo.Dequeue());  // send it to the aliveAmmo queue.
                shoot3Bullets(direction, rotation, velocity);



                state = WeaponState.DownTime;
            }

        }


        private void shoot2Bullets(Vector3 direction, float rotation, Vector3 velocity)
        {
            Vector3 perp2 = Vector3.Zero;
            Vector3 perp1 = Vector3.Zero;

            float offset = deadAmmo.Peek().Tex.ScaleX * 2.5f;

            perp1.X = -direction.Y;
            perp2.Y = direction.X;

            perp2.X = direction.Y;
            perp2.Y = -direction.X;


            deadAmmo.Peek().Position = shootingPosition + (perp1 * offset);  // this will set the position of the projectile
            deadAmmo.Peek().Shoot(Shooter, direction, rotation, velocity);
            aliveAmmo.Enqueue(deadAmmo.Dequeue());  // send it to the aliveAmmo queue.


            deadAmmo.Peek().Position = shootingPosition + (perp2 * offset);  // this will set the position of the projectile
            deadAmmo.Peek().Shoot(Shooter, direction, rotation, velocity);
            aliveAmmo.Enqueue(deadAmmo.Dequeue());  // send it to the aliveAmmo queue.

        }

        private void shoot3Bullets(Vector3 direction, float rotation, Vector3 velocity)
        {

            Vector3 perp2 = Vector3.Zero;
            Vector3 perp1 = Vector3.Zero;

            float offset = deadAmmo.Peek().Tex.ScaleX *3f;

            perp1.X = -direction.Y;
            perp2.Y = direction.X;

            perp2.X = direction.Y;
            perp2.Y = -direction.X;


            deadAmmo.Peek().Position = shootingPosition + (perp1 * offset);  // this will set the position of the projectile
            deadAmmo.Peek().Shoot(Shooter, direction, rotation, velocity);
            aliveAmmo.Enqueue(deadAmmo.Dequeue());  // send it to the aliveAmmo queue.


            deadAmmo.Peek().Position = shootingPosition + (perp2 * offset);  // this will set the position of the projectile
            deadAmmo.Peek().Shoot(Shooter, direction, rotation, velocity);
            aliveAmmo.Enqueue(deadAmmo.Dequeue());  // send it to the aliveAmmo queue.
        }

        public override void playWeaponSound()
        {
            int next = rand.Next(0,2);
            if (next == 0)
                AudioManager.Play(missilelaunch1);
            else if (next == 1)
                AudioManager.Play(missilelaunch2);
            else
                AudioManager.Play(missilelaunch3);
        }
	}
}
